package com.jd.dict.helper;

import java.io.*;

public class FileOperator {
	public static void writeFile(String filepath, String content) {
		File file = new File(filepath);
		file.getParentFile().mkdirs();
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(file));
			writer.write(content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String readFile(String filePath) {
		String content = "";
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(filePath);

			BufferedReader bufferedReader = new BufferedReader(fileReader);

			content = bufferedReader.readLine();

			bufferedReader.close();
			fileReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}

	/**
	 * 复制文件夹
	 *
	 * @param resource 源路径
	 * @param target   目标路径
	 */
	public static void copyFolder(String resource, String target) throws Exception {
		File resourceFile = new File(resource);
		if (!resourceFile.exists()) {
			throw new Exception("源目标路径：[" + resource + "] 不存在...");
		}

		copyFolder(resource, target, resourceFile.getName());
	}
	private static void copyFolder(String resource, String target, String rootName) throws Exception {
		File resourceFile = new File(resource);
		if (!resourceFile.exists()) {
			throw new Exception("源目标路径：[" + resource + "] 不存在...");
		}
		File targetFile = new File(target);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 获取源文件夹下的文件夹或文件
		File[] resourceFiles = resourceFile.listFiles();
		String forderName = resourceFile.getName();
		forderName = (forderName.equals(rootName) ? "" : forderName);

		for (File file : resourceFiles) {
			File file1 = new File(targetFile.getAbsolutePath() + File.separator + forderName);
			// 复制文件
			if (file.isFile()) {
				System.out.println("文件" + file.getName());
				// 在 目标文件夹（B） 中 新建 源文件夹（A），然后将文件复制到 A 中
				// 这样 在 B 中 就存在 A
				if (!file1.exists()) {
					file1.mkdirs();
				}
				File targetFile1 = new File(file1.getAbsolutePath() + File.separator + file.getName());
				copyFile(file, targetFile1);
			}
			// 复制文件夹
			if (file.isDirectory()) {// 复制源文件夹
				String dir1 = file.getAbsolutePath();
				// 目的文件夹
				String dir2 = file1.getAbsolutePath();
				copyFolder(dir1, dir2, rootName);
			}
		}


	}

	/**
	 * 复制文件
	 *
	 * @param resource
	 * @param target
	 */
	private static void copyFile(File resource, File target) throws Exception {
		// 输入流 --> 从一个目标读取数据
		// 输出流 --> 向一个目标写入数据

		long start = System.currentTimeMillis();

		// 文件输入流并进行缓冲
		FileInputStream inputStream = new FileInputStream(resource);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

		// 文件输出流并进行缓冲
		FileOutputStream outputStream = new FileOutputStream(target);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

		// 缓冲数组
		// 大文件 可将 1024 * 2 改大一些，但是 并不是越大就越快
		byte[] bytes = new byte[1024 * 2];
		int len = 0;
		while ((len = inputStream.read(bytes)) != -1) {
			bufferedOutputStream.write(bytes, 0, len);
		}
		// 刷新输出缓冲流
		bufferedOutputStream.flush();
		//关闭流
		bufferedInputStream.close();
		bufferedOutputStream.close();
		inputStream.close();
		outputStream.close();
	}

	public static String getTemplatePath() {
		return getProjectRootPath() + File.separator + "template";

	}

	public static String getProjectRootPath() {
		String srcPath = FileOperator.class.getClassLoader().getResource("").getPath();
		srcPath = srcPath.substring(0, srcPath.indexOf("target") - 1);

		return srcPath;
	}
}